layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: 'http://localhost:8080/database/getListData',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {type: "checkbox", width: 50},
                {field: 'dbName', width: 200, title: '数据库名称'},
                {field: 'dbType', width: 200, title: '数据库类型'},
                {field: 'host', width: 150, title: 'host'},
                {field: 'port', width: 150, title: 'port'},
                {field: 'username', width: 100, title: '用户名'},
                {field: 'status', width: 200, title: '连接状态', toolbar: '#connectionStatus', align: "center"},
                {field: 'createDate', width: 200, title: '创建时间', sort: true},
                {field: 'updateDate', width: 200, title: '更新时间', sort: true},
                {title: '操作', minWidth: 150, fixed: "right", toolbar: '#currentTableBar', align: "center"}
            ]
        ],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line',
        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.rows //解析数据列表
            };
        }
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        var result = JSON.stringify(data.field);
        layer.alert(result, {
            title: '最终的搜索信息'
        });

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            var index = layer.open({
                title: '添加数据库',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['600px', '500px'],
                content: 'http://localhost:8080/database/toAddPage',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = list.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        console.log(JSON.stringify(data))
        if (obj.event === 'newTableLoading') {
            console.log(data.field)
            $.ajax({
                type: "POST",
                url: "/database/newTableLoading",
                data: JSON.stringify(data),
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function(result) {
                    if(result && result.code === 0){
                        var index = layer.alert(result.msg, { title: '提示', closeBtn: 0 }, function () {
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            //window.parent.reload()
                            parent.layer.close(iframeIndex);
                            location.reload()
                        });
                    }else if(result && result.msg){
                        layer.alert(result.msg, { title: '提示' });
                    }else {
                        layer.alert("保存失败", { title: '提示' });
                    }
                }
            });
        }else if (obj.event === 'edit') {

            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/layui/page/table/edit.html',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });


});